package com.societyGames.utility
{
	public class PointPacker
	{
		public static const MIN_VALUE:int = -32768;
		public static const MAX_VALUE:int = 32767;

		public static function pack(x:int, y:int):uint
		{
			if (x < MIN_VALUE || x > MAX_VALUE 
				|| y < MIN_VALUE || y > MAX_VALUE)
			{
				throw new ArgumentError("Argument is out of range: " + x + "," + y);
			}
			return (x << 16) | ((y << 16) >>> 16);
		}
		
		public static function unpackX(value:uint):int
		{
			return (0xFFFF0000 & value) >> 16;
		}
		
		public static function unpackY(value:uint):int
		{
			return ((0x0000FFFF & value) << 16) >> 16;
		}
		
		public static function testAll():void
		{
			test(MIN_VALUE, MIN_VALUE);
			test(-1, -1);
			test(0, 0);
			test(1, 1);
			test(MAX_VALUE, MAX_VALUE);
		}
		
		private static function test(x:int, y:int):void
		{
			var point:uint = pack(x, y);
			var unpackedX:int = unpackX(point); 
			if (x != unpackedX)
			{
				throw new Error("Bad: " + x + "," + y);
			}
			var unpackedY:int = unpackY(point);
			if (y != unpackedY)
			{
				throw new Error("Bad: " + x + "," + y);
			}
		}
	}
}